
#include <QApplication>
#include <gui/mainwindow.h>
#include <gui/renderwidget.h>
#include <math/vector.h>
#include <gfx/polyrendermesh.h>
#include <stdio.h>


inka::gfx::PolyRenderMesh* create_mesh ()
{
  inka::math::Vector3f vertices [] = {
    inka::math::Vector3f (-100.0f,  100.0f, 0.0f),
    inka::math::Vector3f ( -33.0f,  100.0f, 0.0f),
    inka::math::Vector3f (  33.0f,  100.0f, 0.0f),
    inka::math::Vector3f ( 100.0f,  100.0f, 0.0f),

    inka::math::Vector3f (-100.0f,   33.0f, 0.0f),
    inka::math::Vector3f ( -33.0f,   33.0f, 0.0f),
    inka::math::Vector3f (  33.0f,   33.0f, 0.0f),
    inka::math::Vector3f ( 100.0f,   33.0f, 0.0f),

    inka::math::Vector3f (-100.0f,  -33.0f, 0.0f),
    inka::math::Vector3f ( -33.0f,  -33.0f, 0.0f),
    inka::math::Vector3f (  33.0f,  -33.0f, 0.0f),
    inka::math::Vector3f ( 100.0f,  -33.0f, 0.0f),



    inka::math::Vector3f (-100.0f, -100.0f, 0.0f),
    inka::math::Vector3f ( -33.0f, -100.0f, 0.0f),
    inka::math::Vector3f (  33.0f, -100.0f, 0.0f),
    inka::math::Vector3f ( 100.0f, -100.0f, 0.0f),




  };
  inka::math::Vector3f normals[] = {
    inka::math::Vector3f (0.0f, 0.0f, 1.0f),
    inka::math::Vector3f (0.0f, 0.0f, 1.0f),
    inka::math::Vector3f (0.0f, 0.0f, 1.0f),
    inka::math::Vector3f (0.0f, 0.0f, 1.0f),
    inka::math::Vector3f (0.0f, 0.0f, 1.0f),
    inka::math::Vector3f (0.0f, 0.0f, 1.0f),
    inka::math::Vector3f (0.0f, 0.0f, 1.0f),
    inka::math::Vector3f (0.0f, 0.0f, 1.0f),
    inka::math::Vector3f (0.0f, 0.0f, 1.0f),
    inka::math::Vector3f (0.0f, 0.0f, 1.0f),
    inka::math::Vector3f (0.0f, 0.0f, 1.0f),
    inka::math::Vector3f (0.0f, 0.0f, 1.0f),
    inka::math::Vector3f (0.0f, 0.0f, 1.0f),
    inka::math::Vector3f (0.0f, 0.0f, 1.0f),
    inka::math::Vector3f (0.0f, 0.0f, 1.0f),
    inka::math::Vector3f (0.0f, 0.0f, 1.0f),
  };
  inka::math::Vector2f texCoord[] = {
    inka::math::Vector2f (0.0f,  1.0f),
    inka::math::Vector2f (0.33f, 1.0f),
    inka::math::Vector2f (0.66f, 1.0f),
    inka::math::Vector2f (1.0f,  1.0f),

    inka::math::Vector2f (0.0f,  0.66f),
    inka::math::Vector2f (0.33f, 0.66f),
    inka::math::Vector2f (0.66f, 0.66f),
    inka::math::Vector2f (1.0f,  0.66f),

    inka::math::Vector2f (0.0f,  0.33f),
    inka::math::Vector2f (0.33f, 0.33f),
    inka::math::Vector2f (0.66f, 0.33f),
    inka::math::Vector2f (1.0f,  0.33f),

    inka::math::Vector2f (0.0f,  0.0f),
    inka::math::Vector2f (0.33f, 0.0f),
    inka::math::Vector2f (0.66f, 0.0f),
    inka::math::Vector2f (1.0f,  0.0f),
  };

  float selections [] = {
      0.0f, 0.0f, 0.0f, 0.0f,
      0.0f, 1.0f, 1.0f, 0.0f,
      0.0f, 1.0f, 1.0f, 1.0f,
      0.0f, 0.0f, 0.0f, 0.0f
  };

#define QUAD(b)  b+0, b+1, b+4,   b+1, b+4, b+5

  unsigned indices[] = {
    QUAD(0), QUAD(1), QUAD(2),
    QUAD(4), QUAD(5), QUAD(6),
    QUAD(8), QUAD(9), QUAD(10),
  };
#undef QUAD

  inka::gfx::VertexBuffer* vertexBuffer = new inka::gfx::VertexBuffer ();
  vertexBuffer->SetData(vertices, sizeof(vertices));

  inka::gfx::VertexBuffer* normalBuffer = new inka::gfx::VertexBuffer ();
  normalBuffer->SetData(normals, sizeof(normals));

  inka::gfx::VertexBuffer* texCoordBuffer = new inka::gfx::VertexBuffer ();
  texCoordBuffer->SetData(texCoord, sizeof(texCoord));

  inka::gfx::VertexBuffer* selectionBuffer = new inka::gfx::VertexBuffer ();
  selectionBuffer->SetData(selections, sizeof(selections));

  inka::gfx::IndexBuffer* indexBuffer = new inka::gfx::IndexBuffer ();
  indexBuffer->SetData(indices, sizeof(indices));

  inka::gfx::PolyRenderMesh* renderMesh = new inka::gfx::PolyRenderMesh ();
  renderMesh->SetVertices(sizeof(vertices) / sizeof(inka::math::Vector3f), vertexBuffer, normalBuffer, texCoordBuffer);
  renderMesh->SetSelections(selectionBuffer);
  renderMesh->SetIndices(sizeof(indices) / sizeof(unsigned), indexBuffer);

  return renderMesh;
}

int main (int argc, char** argv)
{
  QApplication app (argc, argv);


  inka::gui::MainWindow win;
  win.setVisible(true);
  fflush(stdout);

  win.GetRenderWidget()->makeCurrent();

  inka::gfx::PolyRenderMesh* mesh = create_mesh();
  win.GetRenderWidget()->SetRenderMesh(mesh);


  return app.exec();
}
